package cn.edu.nwpu.warehousemanagement.dao;

import cn.edu.nwpu.warehousemanagement.pojo.Shelf;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;
import java.util.Set;

@Repository
public interface ShelfRepository extends JpaRepository<Shelf, Long> {

    // ==================== 基础查询 ====================
    Optional<Shelf> findByIdAndDeletedFalse(Long id);
    Optional<Shelf> findByNameAndDeletedFalse(String name);
    List<Shelf> findByNameContainingAndDeletedFalse(String name);
    List<Shelf> findByNameContainingIgnoreCaseAndDeletedFalse(String name);

    // 位置查询方法
    List<Shelf> findByLocationAndDeletedFalse(String location);
    List<Shelf> findByLocationContainingIgnoreCaseAndDeletedFalse(String location);

    // 组合查询方法名和参数
    List<Shelf> findByNameAndLocationAndDeletedFalse(String name, String location);

    // ==================== 更新操作 ====================
    @Modifying
    @Transactional
    @Query("UPDATE Shelf s SET s.name = :name WHERE s.id = :id AND s.deleted = false")
    int updateNameById(@Param("id") Long id, @Param("name") String name);

    // 修复位置更新方法名和字段名
    @Modifying
    @Transactional
    @Query("UPDATE Shelf s SET s.location = :location WHERE s.id = :id AND s.deleted = false")
    int updateLocationById(@Param("id") Long id, @Param("location") String location);

    // ==================== 删除操作 ====================
    @Modifying
    @Transactional
    @Query("UPDATE Shelf s SET s.deleted = true WHERE s.id = :id")
    void softDeleteById(@Param("id") Long id);

    @Modifying
    @Transactional
    @Query("UPDATE Shelf s SET s.deleted = true WHERE s.name = :name")
    void softDeleteByName(@Param("name") String name);

    // ==================== 存在性检查 ====================
    boolean existsByNameAndDeletedFalse(String name);

    // ==================== 分页查询 ====================
    Page<Shelf> findAllByDeletedFalse(Pageable pageable);
    List<Shelf> findAllByDeletedFalse();

    @Query("SELECT s FROM Shelf s WHERE s.deleted = false ORDER BY s.name")
    Page<Shelf> findAllOrderByName(Pageable pageable);

    Page<Shelf> findByNameContainingAndDeletedFalse(String name, Pageable pageable);

    // ==================== 包含已删除记录的查询 ====================
    @Query("SELECT s FROM Shelf s")
    List<Shelf> findAllIncludingDeleted();

    @Query("SELECT s FROM Shelf s WHERE s.id = :id")
    Optional<Shelf> findByIdIncludingDeleted(@Param("id") Long id);

    @Modifying
    @Transactional
    @Query("UPDATE Shelf s SET s.deleted = false WHERE s.id = :id")
    void restoreById(@Param("id") Long id);

    // 根据ID集合查询未删除的货架
    List<Shelf> findByIdInAndDeletedFalse(Set<Long> ids);
}